import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    component: () => import('../views/Index.vue'),
    redirect: '/shipu',
    children: [
      {
        path: '/shipu',
        component: () => import('../components/index/Shipu.vue')
      },
      {
        path: '/shipu/:id',
        component: () => import('../components/index/ShipuInfo.vue')
      },
      {
        path: '/shipu/select/:skey',
        component: () => import('../components/index/ShipuSelect.vue')
      },
      {
        path: '/shicai',
        component: () => import('../components/index/Shicai.vue')
      },
      {
        path: '/shicai/:id',
        component: () => import('../components/index/ShicaiInfo.vue')
      },
      {
        path: '/caidan',
        component: () => import('../components/index/Caidan.vue')
      },
      {
        path: '/caidan/:id',
        component: () => import('../components/index/CaidanInfo.vue')
      },
      {
        path: '/publish',
        component: () => import('../components/index/ShiPuFaBu.vue')
      },
      {
        path: 'dongtai',
        component: () => import('../components/index/Dongtai.vue')
      },
      {
        path: '/user/:id',
        component: () => import('../components/index/Me.vue'),
        redirect: '/user/:id/home',
        children: [
          {
            path: 'caidan',
            component: () => import('../components/index/userhome/Caidan.vue')
          },

          {
            path: 'guanxi/:type',
            component: () => import('../components/index/userhome/Guanxi.vue')
          },
          {
            path: 'home',
            component: () => import('../components/index/userhome/Home.vue')
          },
          {
            path: 'shipu',
            component: () => import('../components/index/userhome/Shipu.vue')
          },
          {
            path: 'shoucang',
            component: () => import('../components/index/userhome/Shoucang.vue')
          },
        ],
      },
    ],
  },
  {
    path: '/admin',
    component: () => import('../views/Admin.vue')
  },
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    component: () => import('../views/Register.vue')
  },

]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {

  if (to.path == from.path) {
    return
  } else {
    next()
  }
  // if (to.path == '/login') {
  //     window.localStorage.setItem('preLogin', from.path)
  // }

  // if (from.path == '/' && to.path == '/movie') {
  //     window.localStorage.setItem('menuIndex', '/movie')
  // }


})

router.afterEach((to, from) => {
  // 让页面回到顶部
  let x = document.getElementsByClassName('backtop')
  for (const i of x) {
    if (i.scrollTop) {
      i.scrollTop = 0
    }
  }
})


export default router
